package com.cgbl.cg.ui.mine

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.CourseAdapter
import com.cgbl.cg.databinding.AppActivityCourseCenterBinding
import com.cgbl.cg.vm.mine.CourseCenterViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.model.CourseBean
import com.robot.base.model.CourseInfoBean
import com.robot.base.util.arouter.ARouterUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener

@Route(path = AppConstants.Router.Mine.A_MINE_COURSE_CENTER)
class CourseCenterActivity :
    GLBaseActivity<AppActivityCourseCenterBinding, CourseCenterViewModel>() {

    lateinit var courseAdapter: CourseAdapter

    override fun initComponents() {
        initView()
        viewModel.refresh()
    }

    private fun initView() {
        courseAdapter = CourseAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(this@CourseCenterActivity)
            adapter = courseAdapter
        }
        courseAdapter.setOnItemClickListener { adapter, view, position ->
            ARouterUtil.goToActivityWithBundle(
                AppConstants.Router.Mine.A_MINE_COURSE_PROFILE,
                Bundle().apply {
                    putString(AppConstants.BundleKey.ID, courseAdapter.data[position].courseId)
                })
        }
        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.refresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
            }
        })
    }

    override fun initViewObservable() {
        viewModel.uc.courseInfoData.observe(this) {
            if (it.isEmpty()) {
                val emptyView = layoutInflater.inflate(R.layout.app_layout_empty, null)
                courseAdapter.setEmptyView(emptyView)
            }
            courseAdapter.setNewInstance(it as MutableList<CourseBean>?)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.loadMoreCourseInfoData.observe(this) {
            courseAdapter.addData(it)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
    }

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_course_center

    override fun initVariableId(): Int = BR.viewModel
}